package com.ebupt.migu.music.autoTri.Service.Impl;

import com.ebupt.migu.music.autoTri.Mapper.EffectTotalMapper;
import com.ebupt.migu.music.autoTri.Service.EffectTotalService;
import com.ebupt.migu.music.common.entity.Page;
import com.ebupt.migu.music.common.entity.ResultObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author tz
 */
@Slf4j
@Service
public class EffectTotalServiceImpl implements EffectTotalService {

    @Autowired
    private EffectTotalMapper effectTotalMapper;

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Override
    public ResultObject qryList(Map<String, Object> param) {
        int total = 0;
        log.info("自动运营效果统计请求参数：{}", param);
        List<Map<String, Object>> list = null;
        ResultObject<Page<Map<String, Object>>> res = new ResultObject<>();
        if ("0".equals(param.get("summary"))) {
            // 策略类型
            String tacMode = param.get("tacMode").toString();
            // 分页查询结果集
            switch (tacMode) {
                case "1":
                    total = effectTotalMapper.qryCount(param);
                    list = effectTotalMapper.qryCenSmsList(param);
                    break;
                case "2":
                    total = effectTotalMapper.qryAutoOperCenPushCount(param);
                    list = effectTotalMapper.qryCenPushList(param);
                    break;
                case "3":
                    total = effectTotalMapper.qryCount(param);
                    list = effectTotalMapper.qryCenMsgList(param);
                    break;
                default:
                    break;
            }
        } else {
            // 总数
            total = effectTotalMapper.getCount(param);
            // 策略类型
            String tacMode = param.get("tacMode").toString();
            // 分页查询结果集
            switch (tacMode) {
                case "1":
                    list = effectTotalMapper.getCenSmsList(param);
                    break;
                case "2":
                    list = effectTotalMapper.getCenPushList(param);
                    break;
                case "3":
                    list = effectTotalMapper.getCenMsgList(param);
                    break;
                default:
                    break;
            }
        }
        // 分页
        Page<Map<String, Object>> page = new Page<>();
        page.setTotal(total);
        page.setData(list);
        res.setData(page);
        return res;
    }

    @Override
    public ResultObject qrySum(Map<String, Object> param) {
        ResultObject res = new ResultObject();
        // 策略类型
        String tacMode = param.get("tacMode").toString();
        // 查询结果集
        Map<String, Object> result = new HashMap<>(2);
        List<Map<String, Object>> list = null;
        Map<String, Object> map = null;
        switch (tacMode) {
            case "1":
                list = effectTotalMapper.cenSms(param);
                map = effectTotalMapper.cenSmsTotal(param);
                break;
            case "2":
                list = effectTotalMapper.cenPush(param);
                map = effectTotalMapper.cenPushTotal(param);
                break;
            case "3":
                list = effectTotalMapper.cenPopup(param);
                map = effectTotalMapper.cenPopupTotal(param);
                break;
            case "4":
                list = effectTotalMapper.cenMsg(param);
                map = effectTotalMapper.cenMsgTotal(param);
                break;
            default:
                break;
        }
        result.put("table", list);
        result.put("title", map);
        res.setMsg("查询echart图表成功");
        res.setData(result);
        return res;
    }

    @Override
    public List<Map<String, Object>> export(Map<String, Object> param) {
        // 结果集
        List<Map<String, Object>> list = null;
        // 策略类型
        String tacMode = param.get("tacMode").toString();

        if ("0".equals(param.get("summary"))) {
            switch (tacMode) {
                case "1":
                    list = effectTotalMapper.qryCenSmsList(param);
                    break;
                case "2":
                    list = effectTotalMapper.qryCenPushList(param);
                    break;
                case "3":
                    list = effectTotalMapper.qryCenMsgList(param);
                    break;
                default:
                    return null;
            }
        } else {
            switch (tacMode) {
                case "1":
                    list = effectTotalMapper.getCenSmsList(param);
                    break;
                case "2":
                    list = effectTotalMapper.getCenPushList(param);
                    break;
                case "3":
                    list = effectTotalMapper.getCenMsgList(param);
                    break;
                default:
                    return null;
            }
        }
        String dateStr;
        for (Map map : list) {
            // 去除时间戳的尾部的0
            dateStr = map.get("date").toString();
            map.put("date", dateStr.substring(0, dateStr.length() - 2));
        }
        return list;
    }


}
